package com.yanqu.road.server.manager.config;

import com.yanqu.road.entity.config.goods.GoodsCompose;
import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.entity.enums.eGoodsType;
import com.yanqu.road.logic.bussiness.config.GoodsBussiness;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.manger.TempCommonMgr;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Description
 * @Author cwq
 * @Data 2021/7/23 17:55
 */
public class GoodsMgr extends TempMgr {

    private static Map<Integer, GoodsInfo> goodsInfoMap;

    private static Map<Integer, GoodsCompose> goodsComposeMap;
    /**
     * 合成道具，子道具，合成道具需要多少子道具
     */
    private static Map<Integer, Map<Integer, BigInteger>> allGoodsComposeMap;

    /**
     * 荣誉点类型  旗帜解锁条件  对应关系
     */
    private static Map<Integer, List<GoodsInfo>> flagUnLockMap;
    @Override
    public boolean stop() {
        return false;
    }

    @Override
    public boolean save() {
        return false;
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() {
        reloadGoodsInfo();
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    public static void reloadGoodsInfo() {
        Map<Integer, GoodsInfo> tempGoodsInfoMap = GoodsBussiness.getGoodsInfoMap();
        TempCommonMgr.initGoodsInfoMap(tempGoodsInfoMap);
        goodsInfoMap = tempGoodsInfoMap;
        goodsComposeMap = GoodsBussiness.getGoodsComposeMap();
        allGoodsComposeMap = initGoodsComposeMap();
        flagUnLockMap = initFlagUnLockMap();
    }

    private static Map<Integer, List<GoodsInfo>> initFlagUnLockMap() {
        Map<Integer, List<GoodsInfo>> tempMap = new ConcurrentHashMap<>();
        for (Map.Entry<Integer, GoodsInfo> infoEntry : goodsInfoMap.entrySet()) {
            if(infoEntry.getValue().getType() == eGoodsType.UnionFlagParts.getValue()){
                int honorsType = infoEntry.getValue().getParamList().get(0).intValue();
                if(!tempMap.containsKey(honorsType)){
                    tempMap.put(honorsType, new ArrayList<>());
                }
                tempMap.get(honorsType).add(infoEntry.getValue());
            }
        }
        return tempMap;
    }

    public static List<GoodsInfo> getFiveHeroAddItem() {
        List<GoodsInfo> list = new ArrayList<>();
        List<GoodsInfo> goodsInfoList = TempCommonMgr.getGoodsInfoList();
        for(GoodsInfo goods : goodsInfoList){
            if (goods != null && goods.getType() == eGoodsType.FiveHeroAddItem.getValue()) {
                list.add(goods);
            }
        }
        return list;
    }

    public static Map<Integer, GoodsInfo> getGoodsInfoMap() {
        return goodsInfoMap;
    }

    /**
     * 获取物品信息 包含本地物品(t_p_goods)和远程物品(t_s_goods、t_s_goods_channel)
     * @param goodId
     * @return
     */
    public static GoodsInfo getGoodsInfo(int goodId){
        GoodsInfo goodsInfo = TempCommonMgr.getGoodsInfo(goodId);
        if(goodsInfo == null){
            goodsInfo = GoodsMgr.getGoodsInfoMap().get(goodId);
        }
        return goodsInfo;
    }

    /**
     * 初始化道具合成数量
     *
     * @return
     */
    private static Map<Integer, Map<Integer, BigInteger>> initGoodsComposeMap() {
        Map<Integer, Map<Integer, BigInteger>> dataMap = new ConcurrentHashMap<>();
        for (GoodsCompose goodsCompose : goodsComposeMap.values()) {
            Map<Integer, BigInteger> tempDataMap = dataMap.get(goodsCompose.getGoodsId());
            if (null == tempDataMap) {
                tempDataMap = new ConcurrentHashMap<>();
                dataMap.put(goodsCompose.getGoodsId(), tempDataMap);
            }
            int goodsId = 0;
            BigInteger count = BigInteger.ZERO;
            for (Map.Entry<Integer, BigInteger> dataEntry : goodsCompose.getConsume().getGoods().entrySet()) {
                goodsId = dataEntry.getKey();
                count = dataEntry.getValue();
            }
            tempDataMap.put(goodsId, count);
        }
        Map<Integer, Map<Integer, BigInteger>> resultMap = new ConcurrentHashMap<>();
        for (Map.Entry<Integer, Map<Integer, BigInteger>> dataEntry : dataMap.entrySet()) {
            Map<Integer, BigInteger> inResultMap = new ConcurrentHashMap<>();
            resultMap.put(dataEntry.getKey(), inResultMap);
            for (Map.Entry<Integer, BigInteger> tempEntry : dataEntry.getValue().entrySet()) {
                setInMap(inResultMap, dataMap, tempEntry.getKey(), tempEntry.getValue());
            }
        }
        return resultMap;
    }

    private static void setInMap(Map<Integer, BigInteger> resultMap, Map<Integer, Map<Integer, BigInteger>> dataMap, int goodsId, BigInteger count) {
        resultMap.put(goodsId, count);
        Map<Integer, BigInteger> tempMap = dataMap.get(goodsId);
        if (null != tempMap) {
            for (Map.Entry<Integer, BigInteger> inEntry : tempMap.entrySet()) {
                setInMap(resultMap, dataMap, inEntry.getKey(), count.multiply(inEntry.getValue()));
            }
        }
    }

    /**
     * 获取道具合成数量
     *
     * @param goodsId
     * @param childGoodsId
     * @return
     */
    public static BigInteger getGoodsComposeCount(int goodsId, int childGoodsId) {
        Map<Integer, BigInteger> tempMap = allGoodsComposeMap.get(goodsId);
        if (null != tempMap) {
            return tempMap.get(childGoodsId);
        }
        return null;
    }

    /**
     * 获取会旗的持续时间
     *
     * @param flagId
     * @return 返回毫秒
     */
    public static long getUnionFlagDurationTime(int flagId) {
        GoodsInfo goods = getGoodsInfo(flagId);
        if(goods != null && goods.getType() == eGoodsType.UnionFlagParts.getValue()){
            //永久
            return -1;
        }
        if (goods == null || goods.getType() != eGoodsType.UnionFlag.getValue()) {
            return 0;
        }
        return goods.getParamList().get(0).longValue() * 60 * 1000;
    }


    public static boolean isUnionFlag(int goodsId) {
        GoodsInfo goods = getGoodsInfo(goodsId);
        if (goods == null || goods.getType() != eGoodsType.UnionFlag.getValue()) {
            return false;
        }
        return true;
    }

    public static boolean isUnionFlagParts(int goodsId) {
        GoodsInfo goods = getGoodsInfo(goodsId);
        if (goods == null || goods.getType() != eGoodsType.UnionFlagParts.getValue()) {
            return false;
        }
        return true;
    }

    /**
     * 是否是荣誉点积分
     */
    public static boolean isHonorScore(int goodsId){
        GoodsInfo goods = getGoodsInfo(goodsId);
        if (goods == null || goods.getType() != eGoodsType.UnionHonorScore.getValue()) {
            return false;
        }
        return true;
    }


    public static List<GoodsInfo> getHonorsFlag(int type){
        return flagUnLockMap.get(type);
    }

    /**
     * 是否是五熊真棒的技能道具
     */
    public static boolean isFiveHeroAddItem(int goodsId){
        GoodsInfo goods = getGoodsInfo(goodsId);
        if (goods == null || goods.getType() != eGoodsType.FiveHeroAddItem.getValue()) {
            return false;
        }
        return true;
    }

    /**
     * 是否饰品
     */
    public static boolean isDecorationGoods(int goodsId) {
        return goodsId >= 106000 && goodsId <= 106999;
    }

}
